#https://blog.51cto.com/u_16175449/11271189
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import text
from sqlalchemy import create_engine
from mysqlconn import getSession
from sqlalchemy.ext.automap import automap_base
dbHost = 'mysql+pymysql://root:123456@127.0.0.1:3306/hzrag'
engine = create_engine(
    dbHost,
    echo=True,  # 是否打印SQL
    pool_size=10,  # 连接池的大小，指定同时在连接池中保持的数据库连接数，默认:5
    max_overflow=20,  # 超出连接池大小的连接数，超过这个数量的连接将被丢弃,默认: 5
)
Base = automap_base() 
Base.prepare(engine, reflect=True)

File = Base.classes.t_file


Session = sessionmaker(bind=engine)
session = Session()

# Insert a new user
new_user = File(name='Alice')
result=session.add(new_user)
session.flush()  # 确保数据被写入数据库但不提交事务
last_id = session.connection().connection.insert_id()  # 获取最后插入行的自增ID
print("Last Insert ID:", last_id)
session.commit()